'use strict'
// const autoprefixer = require('autoprefixer');
// const pxtorem = require('postcss-pxtorem');
// const proTit = require('./utils/config.js')

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
//const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const {
	proTit
} = require('./src/utils/config.js')
const path = require('path');
const port = process.env.port || process.env.npm_config_port || 8080 // dev port
function resolve(dir) {
	return path.join(__dirname, dir)
}
let {
	// VUE_APP_PROSRC: BASE_URL,
	NODE_ENV,ENV
} = process.env
console.log('NODE_ENV22222--------------------', ENV,NODE_ENV,'tit',proTit)
const prod = NODE_ENV === 'production'

//const Timestamp = new Date().getTime() //当前时间为了防止打包缓存不刷新，所以给每个js文件都加一个时间戳
//const Version = process.env.VUE_APP_Version //当前版本号

// const proTit = prod? '/hkmobile' : '/hkmobile'
let cdn = {
	css: ['//cdn.jsdelivr.net/npm/vant/lib/index.css'],
	js: [
		'//cdn.jsdelivr.net/npm/vue/dist/vue.min.js',
		'//cdn.jsdelivr.net/npm/vue-router/dist/vue-router.min.js',
		'//cdn.jsdelivr.net/npm/vuex/dist/vuex.min.js',
		'//cdn.jsdelivr.net/npm/axios/dist/axios.min.js',
		'//cdn.jsdelivr.net/npm/vant/lib/vant.min.js'
	]
}
module.exports = {
	outputDir: 'D:/productTool/xampp/htdocs/'+proTit,
	publicPath: proTit,
	productionSourceMap: false,
	lintOnSave: process.env.NODE_ENV === 'development',
	
	devServer: {
		port: port,
		open: true,
		overlay: {
				warnings: false,
				errors: true,
				drop_debugger: true,
				drop_console: true,
		},
		proxy: {
			// change xxx-api/login => mock/login
			// detail: https://cli.vuejs.org/config/#devserver-proxy
			[process.env.VUE_APP_BASE_API]: {
				target: `http://127.0.0.1:${port}`,
				changeOrigin: true,
				pathRewrite: {
					['^' + process.env.VUE_APP_BASE_API]: ''
				}
			}
		},
		// after: require('./mock/mock-server.js')
	},
	css: {
		loaderOptions: {
			postcss: {
				plugins: [
					//  autoprefixer(),
					// pxtorem({
					//   rootValue: 75,
					//   propList: ['*']
					// })
				]
			},
			less: {
				modifyVars: {
					'hack': `true; @import "${resolve('src/style/them.less')}";`
				}
			},
		}
	},
	configureWebpack: (config) => {
		// config.output = {
		// 	// 输出重构  打包编译后的 文件名称  【模块名称.版本号.时间戳】
		// 	filename: `js/[name].${Version}.${Timestamp}.js`,
		// 	chunkFilename: `js/[name].${Version}.${Timestamp}.js`
		// 	}
		
		if (prod) {
			config.externals = {
				'vue': "Vue",
				'vuex': "Vuex",
				'vue-router': "VueRouter",
				'axios': "axios",
				'vant': "vant"
			}
			config.plugins.push(new BundleAnalyzerPlugin())
		}

	},
	chainWebpack(config) {
		// 配置别名
		config.resolve.alias
			.set("@", resolve("src"))
			.set("@img", resolve("src/assets/images"))
			.set("@js", resolve("src/assets/js"))
			.set("components",resolve("src/components"))
			.set("@less", resolve("src/style"));
		if (prod) {
			// 删除预加载
			config.plugins.delete('preload');
			config.plugins.delete('prefetch');
			// 压缩代码
			config.optimization.minimize(true);
			// 分割代码
			config.optimization.splitChunks({
				chunks: 'all'
			})
			config.plugin('html')
				.tap(args => {
					args[0].cdn = cdn;
					return args;
				});
			// 生产环境注入cdn
			// config.plugin('html')
			// 	.tap(args => {
			// 			args[0].cdn = [cdn[4]]
			// 		return args;
			// 	});
		}else{
			// config.plugin('html')
			// 	.tap(args => {
			// 		args[0].cdn = cdn;
			// 		return args;
			// 	});
		}
	
		// config.plugins.delete('preload') // TODO: need test
		// config.plugins.delete('prefetch') // TODO: need test
		// set svg-sprite-loader
		config.module
			.rule('svg')
			.exclude.add(resolve('src/icons'))
			.end()
		config.module
			.rule('icons')
			.test(/\.svg$/)
			.include.add(resolve('src/icons'))
			.end()
			.use('svg-sprite-loader')
			.loader('svg-sprite-loader')
			.options({
				symbolId: 'icon-[name]'
			})
			.end()

		// set preserveWhitespace
		config.module
			.rule('vue')
			.use('vue-loader')
			.loader('vue-loader')
			.tap(options => {
				options.compilerOptions.preserveWhitespace = true
				return options
			})
			.end()
		config
			// https://webpack.js.org/configuration/devtool/#development
			.when(prod,
				config => config.devtool('cheap-source-map')
			)

		config
			.when(prod,
				config => {
					config
						.plugin('ScriptExtHtmlWebpackPlugin')
						.after('html')
						.use('script-ext-html-webpack-plugin', [{
							// `runtime` must same as runtimeChunk name. default is `runtime`
							inline: /runtime\..*\.js$/
						}])
						.end()
					config
						.optimization.splitChunks({
							chunks: 'all',
							cacheGroups: {
								vant: {
									name: 'chunk-vant', // split vant into a single package
									priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
									test: /[\\/]node_modules[\\/]_?van(.*)/ // in order to adapt to cnpm
								},
								components: {
									name: 'chunk-components',
									test: resolve('src/components'), // can customize your rules
									minChunks: 3, //  minimum common number
									priority: 5,
									reuseExistingChunk: true
								},
								libs: {
									name: 'chunk-libs',
									test: /[\\/]node_modules[\\/]/,
									priority: 10,
									chunks: 'initial' // only package third parties that are initially dependent
								}

							}
						})
					config.optimization.runtimeChunk('single')
				}
			)
	}

}
